
var currentMove = 1;
var autoplayEnabled = true;
var buttonClicked=false;
var countdown;
function prepareDivs() {
	createIds('#table', 'id');
	createIds('#table2', 'enemyid');
}

function createIds(selector, name) {
	$(selector).find('div').each(function(index) {
		var id = name + index;
		$(this).attr('id', id);
	});
}
function strike(targetId, health, dir, len) {
	if (health > 0) {

		$('#enemy' + targetId).html(
				'<img class="missOpponent" src="images/hit.gif"/>');
	} else if (health == 0) {
		clearDivsEnemy(targetId, len, dir);
		if (dir) {
			$('#enemy' + targetId).html(
					'<img class="missOpponent" src="images/' + len
							+ 'point.png" />').animate({
				opacity : 0.4
			}, 800);

		} else {
			$('#enemy' + targetId).html(
					'<img class="missOpponent" src="images/' + len
							+ 'pointR.png" />').animate({
				opacity : 0.4
			}, 800);
		}

	} else {
		$('#enemy' + targetId).prepend(
				'<img class="missOpponent" src="images/miss.png"/>').animate({
			opacity : 0.6
		}, 800);
	}

}

function strike1(targetId, health, dir, len) {
	if (health > 0) {

		$('#' + targetId).html(
				'<img class="missOpponent" src="images/hit.gif"/>');
	} else if (health == 0) {
		clearDivsMy(targetId, len, dir);
		if (dir) {
			$('#' + targetId).html(
					'<img class="missOpponent" src="images/' + len
							+ 'point.png" />').animate({
				opacity : 0.4
			}, 800);

		} else {
			$('#' + targetId).html(
					'<img class="missOpponent" src="images/' + len
							+ 'pointR.png" />').animate({
				opacity : 0.4
			}, 800);
		}

	} else {
		$('#' + targetId).prepend(
				'<img class="missOpponent" src="images/miss.png"/>').animate({
			opacity : 0.6
		}, 800);

	}

}

function stepBack() {
	clearInterval(countdown);
	$('#table').find('div').each(function() {
		$(this).html("");

	});
	$('#table2').find('div').each(function() {
		$(this).html("");
	});
	var kk = currentMove - 1;
	currentMove = 1;
	if (currentMove > 1) {
		$('#btn1').removeAttr("disabled");
	} else {
		$('#btn1').attr("disabled", 'disabled');
		$('#btn3').removeAttr("disabled");
		$('#btn2').removeAttr("disabled");
		autoplayEnabled = true;
		buttonClicked = false;

	}
	for (var i = 1; i < kk; i++) {
		stepFwd();
	}
}

function clearDivsMy(id, len, dir) {
	
	var firstId = parseInt(id.replace("id", ""));

	for ( var i = 0; i < len; i++) {
		if (dir) {
			$("#id" + (firstId + i)).html('');
		} else {
			$("#id" + (firstId + i * 10)).html('');
		}

	}

}
function clearDivsEnemy(id, len, dir) {
	
	var firstId = parseInt(id.replace("id", ""));

	for ( var i = 0; i < len; i++) {
		if (dir) {
			$("#enemyid" + (firstId + i)).html('');
		} else {
			$("#enemyid" + (firstId + i * 10)).html('');
		}

	}

}
function autoplay(){
	buttonClicked=true;
	play();
}

function play() {
	countdown = setInterval(function() {
		if (autoplayEnabled && buttonClicked) {
			stepFwd();
		} else {
			return false;
		}
	}, 1000);
}

function stepFwd() {
	var moveId = localStorage["g" + gameIndex + "move" + currentMove];
	moveId = JSON.parse(moveId);
	if (moveId["x"] == 1) {
		strike1(moveId["cellId"], moveId["health"], moveId["dir"],
				moveId["len"]);
	} else {
		strike(moveId["cellId"], moveId["health"], moveId["dir"], moveId["len"]);
	}
	currentMove++;

	if (currentMove > 1) {
		$('#btn1').removeAttr("disabled");
	} else {
		$('#btn1').attr("disabled", 'disabled');
	}
	var moveId = localStorage["g" + gameIndex + "move" + currentMove];
	moveId = JSON.parse(moveId);
	if (moveId == null) {
		clearInterval(countdown);
		$('#btn3').attr("disabled", 'disabled');
		autoplayEnabled = false;
		buttonClicked = false;
		$('#btn2').attr("disabled", 'disabled');
	} else {
		$('#btn3').removeAttr("disabled");
		$('#btn2').removeAttr("disabled");
		autoplayEnabled = true;
		

	}

}